home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / pvm34b3.zip / pvm34b3 / pvm3 / src / pvmlog.c < prev    next >
C/C++ Source or Header  |  1997-08-08  |  10KB  |  473 lines

  1.  
  2. static char rcsid[] =
  3.     "$Id: pvmlog.c,v 1.5 1997/07/11 18:44:52 pvmsrc Exp $";
  4.  
  5. /*
  6.  *         PVM version 3.4:  Parallel Virtual Machine System
  7.  *               University of Tennessee, Knoxville TN.
  8.  *           Oak Ridge National Laboratory, Oak Ridge TN.
  9.  *                   Emory University, Atlanta GA.
  10.  *      Authors:  J. J. Dongarra, G. E. Fagg, M. Fischer
  11.  *          G. A. Geist, J. A. Kohl, R. J. Manchek, P. Mucci,
  12.  *         P. M. Papadopoulos, S. L. Scott, and V. S. Sunderam
  13.  *                   (C) 1997 All Rights Reserved
  14.  *
  15.  *                              NOTICE
  16.  *
  17.  * Permission to use, copy, modify, and distribute this software and
  18.  * its documentation for any purpose and without fee is hereby granted
  19.  * provided that the above copyright notice appear in all copies and
  20.  * that both the copyright notice and this permission notice appear in
  21.  * supporting documentation.
  22.  *
  23.  * Neither the Institutions (Emory University, Oak Ridge National
  24.  * Laboratory, and University of Tennessee) nor the Authors make any
  25.  * representations about the suitability of this software for any
  26.  * purpose.  This software is provided ``as is'' without express or
  27.  * implied warranty.
  28.  *
  29.  * PVM version 3 was funded in part by the U.S. Department of Energy,
  30.  * the National Science Foundation and the State of Tennessee.
  31.  */
  32.  
  33. /*
  34.  *    pvmlog.c
  35.  *
  36.  *    Error logging fac.
  37.  *
  38. $Log: pvmlog.c,v $
  39.  * Revision 1.5  1997/07/11  18:44:52  pvmsrc
  40.  * WIN32 fix for console bug (quit caused halt) - Markus.
  41.  *
  42.  * Revision 1.4  1997/06/27  20:04:54  pvmsrc
  43.  * Integrated WIN32 changes.
  44.  *
  45.  * Revision 1.3  1997/04/30  21:32:48  pvmsrc
  46.  * SGI Compiler Warning Cleanup.
  47.  *     - fixed up return codes for vpvmlogprintf().
  48.  *
  49.  * Revision 1.2  1997/01/28  19:27:29  pvmsrc
  50.  * New Copyright Notice & Authors.
  51.  *
  52.  * Revision 1.1  1996/09/23  23:44:39  pvmsrc
  53.  * Initial revision
  54.  *
  55.  * Revision 1.6  1995/07/28  16:41:00  manchek
  56.  * wrap HASERRORVARS around errno declarations
  57.  *
  58.  * Revision 1.5  1995/05/17  16:44:56  manchek
  59.  * added CSPP subcomplex support (/tmp file names include proc. id).
  60.  * use PVMDLOGMAX envar to set logging limit to /tmp/pvml.uid
  61.  *
  62.  * Revision 1.4  1994/06/03  20:38:25  manchek
  63.  * version 3.3.0
  64.  *
  65.  * Revision 1.3  1993/11/30  15:53:59  manchek
  66.  * added timestamp option
  67.  *
  68.  * Revision 1.2  1993/10/04  20:31:21  manchek
  69.  * renamed useruid to pvm_useruid for compat with libpvm.
  70.  * added alternate logfile name if SHAREDTMP is defined
  71.  *
  72.  * Revision 1.1  1993/08/30  23:26:51  manchek
  73.  * Initial revision
  74.  *
  75.  */
  76.  
  77. #ifdef WIN32
  78. #include "pvmwin.h"
  79. #endif
  80.  
  81. #include <stdio.h>
  82. #include <errno.h>
  83. #include <sys/types.h>
  84. #include <time.h>
  85. #include <fcntl.h>
  86. #ifdef    SYSVSTR
  87. #include <string.h>
  88. #define    CINDEX(s,c)    strchr(s,c)
  89. #else
  90. #include <strings.h>
  91. #define    CINDEX(s,c)    index(s,c)
  92. #endif
  93. #ifdef    __STDC__
  94. #include <stdarg.h>
  95. #else
  96. #include <varargs.h>
  97. #endif
  98.  
  99. #ifndef PVMDLOGMAX
  100. #define PVMDLOGMAX 1000000            /* (approx) max chars to log to file */
  101. #endif
  102.  
  103. /* pvmd log file name */
  104.  
  105. #ifdef    SHAREDTMP
  106. #define    PVMDLOGFILE    "/tmp/pvml.%d.%s"
  107. #else
  108. #ifndef WIN32
  109. #define    PVMDLOGFILE    "/tmp/pvml.%d"
  110. #else
  111. #define PVMDLOGFILE "c:/temp/pvml.%s"  /* for default */
  112. #endif
  113. #endif
  114.  
  115. #ifdef    IMA_CSPP
  116. #ifdef    SHAREDTMP
  117. #define    PVMDLOGFILE_CSPP    "/tmp/pvml.%d.%d.%s"
  118. #else
  119. #define    PVMDLOGFILE_CSPP    "/tmp/pvml.%d.%d"
  120. #endif
  121.  
  122. int get_scid();
  123. #endif    /*IMA_CSPP*/
  124.  
  125.  
  126. /***************
  127.  **  Globals  **
  128.  **           **
  129.  ***************/
  130.  
  131. char *getenv();
  132.  
  133. #ifndef HASERRORVARS
  134. extern int errno;                    /* from libc */
  135. #ifndef WIN32
  136. extern int sys_nerr;
  137. extern char *sys_errlist[];
  138. #endif
  139. #endif
  140.  
  141. #ifdef IMA_OS2
  142. #include<stdlib.h> /* ERRORVARS */
  143. #endif 
  144. extern int pvmmytid;                /* from pvmd.c */
  145. extern int pvmmyupid;                /* from pvmd.c */
  146. extern int pvm_useruid;                /* from pvmd.c */
  147. #ifdef WIN32
  148. extern char *username;                /* from pvmd.c */
  149. extern char *pvmtmpspec;            /* from pvmd.c */
  150. #endif
  151.  
  152. int log_fd = -1;                    /* error log file */
  153.  
  154.  
  155. /***************
  156.  **  Private  **
  157.  **           **
  158.  ***************/
  159.  
  160. static int log_how = 1;                /* how much error logging we can do */
  161. FILE *log_ff = 0;
  162. static int pvmdlogmax = PVMDLOGMAX;
  163. static int atnewline = 1;            /* on new log line (XXX ick) */
  164.  
  165.  
  166. /*    pvmsetlog()
  167. *
  168. *    Set new error logging level.
  169. *    If how & 1, write to stderr
  170. *    If how & 2, write to log file in /tmp
  171. */
  172.  
  173. int
  174. pvmsetlog(how)
  175.     int how;
  176. {
  177.     char buf[160];
  178.     char hna[128];
  179.     char *p;
  180. #ifdef    IMA_CSPP
  181.     int scid = get_scid();    /* default (system) subcomplex ID is 1) */
  182.                             /* differentiate logfile when not on system s/c */
  183. #endif
  184.  
  185.     if (2 & how & ~log_how) {
  186. #ifdef    SHAREDTMP
  187.         if (gethostname(hna, sizeof(hna)-1) == -1) {
  188.             pvmlogerror("pvmsetlog() can't gethostname()\n");
  189.             return 0;
  190.         }
  191.         if (p = CINDEX(hna, '.'))
  192.             *p = 0;
  193. #ifdef    IMA_CSPP
  194.         if (scid > 1)
  195.             (void)sprintf(buf, PVMDLOGFILE_CSPP, pvm_useruid, scid, hna);
  196.         else
  197. #endif
  198.             (void)sprintf(buf, PVMDLOGFILE, pvm_useruid, hna);
  199.  
  200. #else    /*SHAREDTMP*/
  201. #ifdef    IMA_CSPP
  202.         if (scid > 1)
  203.             (void)sprintf(buf, PVMDLOGFILE_CSPP, pvm_useruid, scid);
  204.         else
  205. #endif
  206. #ifndef WIN32
  207.             (void)sprintf(buf, PVMDLOGFILE, pvm_useruid);
  208. #else
  209.         if(!pvmtmpspec) pvmtmpspec = malloc(128*sizeof(char));
  210.  
  211.         if (!getenv("PVM_TMP")) {
  212.             (void)sprintf(buf,PVMDLOGFILE,username);
  213.             fprintf(stderr,
  214.                     "Could not get PVM_TMP, continuing with %s \n",buf);
  215.         } else {
  216.             strcpy(pvmtmpspec,getenv("PVM_TMP"));
  217.             strcat(pvmtmpspec,"/pvml.%s");
  218.             (void)sprintf(buf,pvmtmpspec,username);    
  219.         }
  220. #endif        
  221. #endif    /*SHAREDTMP*/
  222.  
  223.         if ((log_fd = open(buf, O_WRONLY|O_TRUNC|O_CREAT|O_APPEND, 0600)) != -1)
  224.             log_ff = fdopen(log_fd, "a");
  225.         else
  226.             how &= ~2;
  227.  
  228.         if (p = getenv("PVMDLOGMAX"))
  229.             pvmdlogmax = atoi(p);
  230.     }
  231.  
  232.     if (2 & log_how & ~how) {
  233.         (void)fclose(log_ff);
  234.         log_ff = 0;
  235.         log_fd = -1;
  236.     }
  237.  
  238.     log_how = how;
  239.     return 0;
  240. }
  241.  
  242.  
  243. int
  244. vpvmlogprintf(fmt, ap)
  245.     char *fmt;
  246.     va_list ap;
  247. {
  248.     static char *toomuch = "\n*** logging truncated\n";
  249.     static int log_alrdy = 0;    /* how much already written to file */
  250.  
  251.     int cnt = 0;
  252.     int cc;
  253.  
  254. #ifdef    TIMESTAMPLOG
  255.     time_t now;
  256.     struct tm *tmp;
  257.  
  258.     time(&now);
  259.     tmp = localtime(&now);
  260. #endif    /*TIMESTAMPLOG*/
  261.  
  262.     if (log_how & 1) {
  263.         if (atnewline) {
  264.             if (pvmmytid)
  265.                 fprintf(stderr, "[t%x] ", pvmmytid);
  266.             else
  267.                 fprintf(stderr, "[pvmd pid%d] ", pvmmyupid);
  268.  
  269. #ifdef    TIMESTAMPLOG
  270.             fprintf(stderr, "%02d/%02d %02d:%02d:%02d ",
  271.                         tmp->tm_mon + 1,
  272.                         tmp->tm_mday,
  273.                         tmp->tm_hour,
  274.                         tmp->tm_min,
  275.                         tmp->tm_sec);
  276. #endif    /*TIMESTAMPLOG*/
  277.         }
  278.  
  279.         cc = vfprintf(stderr, fmt, ap);
  280.         cnt = ( cc >= 0 ) ? cnt + cc : cc;
  281.         fflush(stderr);
  282.     }
  283.  
  284.     if (log_how & 2) {
  285. /*
  286.         XXX we don't update log_alrdy (log_alrdy < PVMDLOGMAX)
  287. */
  288.         if (atnewline) {
  289.             if (pvmmytid)
  290.                 fprintf(log_ff, "[t%x] ", pvmmytid);
  291.             else
  292.                 fprintf(log_ff, "[pvmd pid%d] ", pvmmyupid);
  293.  
  294. #ifdef    TIMESTAMPLOG
  295.             fprintf(log_ff, "%02d/%02d %02d:%02d:%02d ",
  296.                         tmp->tm_mon + 1,
  297.                         tmp->tm_mday,
  298.                         tmp->tm_hour,
  299.                         tmp->tm_min,
  300.                         tmp->tm_sec);
  301. #endif    /*TIMESTAMPLOG*/
  302.         }
  303.  
  304.         cc = vfprintf(log_ff, fmt, ap);
  305.         cnt = ( cnt >= 0 ) ? ( ( cc >= 0 ) ? cnt + cc : cc ) : cnt;
  306.         fflush(log_ff);
  307.     }
  308.  
  309.     atnewline = (fmt[strlen(fmt) - 1] == '\n') ? 1 : 0;
  310.  
  311. /*
  312.     if ((log_how & 4) && hosts->ht_local != hosts->ht_cons) {
  313. XXX send error-log message to console host
  314.     }
  315. */
  316.  
  317.     return(cnt);
  318. }
  319.  
  320.  
  321. int
  322. #ifdef __STDC__
  323. pvmlogprintf(const char *fmt, ...)
  324. #else
  325. pvmlogprintf(va_alist)
  326.     va_dcl
  327. #endif
  328. {
  329.     va_list ap;
  330.     int cc;
  331.  
  332. #ifdef __STDC__
  333.     va_start(ap, fmt);
  334. #else
  335.     char    *fmt;
  336.  
  337.     va_start(ap);
  338.     fmt = va_arg(ap, char *);
  339. #endif
  340.  
  341.     cc = vpvmlogprintf(fmt, ap);
  342.     va_end(ap);
  343.  
  344.     return cc;
  345. }
  346.  
  347.  
  348. int
  349. pvmlogperror(s)
  350.     char *s;    /* text */
  351. {
  352. #ifdef WIN32
  353.     char em[16];
  354.     int error_nr=0;
  355. #else
  356. #if defined (IMA_FREEBSD) || defined (IMA_OS2)
  357.     const char *em;
  358. #else
  359.     char *em;
  360. #endif
  361. #endif
  362.  
  363. #ifndef WIN32
  364.     em = ((errno >= 0 && errno < sys_nerr)
  365.         ? sys_errlist[errno] : "Unknown Error");
  366. #else
  367.     error_nr = GetLastError();
  368.     sprintf(em,"%d",error_nr);
  369. #endif
  370.     pvmlogprintf("%s: %s\n", s, em);
  371.     return 0;
  372. }
  373.  
  374.  
  375. /*    pvmlogerror()
  376. *
  377. *    Log an error message.  Effect depends on how far we've gotten.
  378. *    If log_how & 1, write to stderr.
  379. *    If log_how & 2, can write to the logfile.
  380. *    If log_how & 4, can send message to console pvmd.
  381. */
  382.  
  383. pvmlogerror(s)
  384.     char *s;    /* text */
  385. {
  386.     pvmlogprintf("%s", s);
  387.     atnewline = 1;
  388.     return 0;
  389.  
  390. #if NOMORELOGERROR
  391.     static char *toomuch = "\n*** logging truncated\n";
  392.     static int log_alrdy = 0;        /* how much already written to file */
  393.     int len = strlen(s);
  394.     char buf[40];
  395.     int l;
  396. #ifdef    TIMESTAMPLOG
  397.     time_t now;
  398.     struct tm *tmp;
  399.  
  400.     time(&now);
  401.     tmp = localtime(&now);
  402. #endif    /*TIMESTAMPLOG*/
  403.  
  404.     if (log_how & 1) {
  405. #ifdef    TIMESTAMPLOG
  406.         if (pvmmytid)
  407.             fprintf(stderr, "[t%x] %02d/%02d %02d:%02d:%02d %s", pvmmytid,
  408.                     tmp->tm_mon + 1,
  409.                     tmp->tm_mday,
  410.                     tmp->tm_hour,
  411.                     tmp->tm_min,
  412.                     tmp->tm_sec,
  413.                     s);
  414.         else
  415.             fprintf(stderr, "[pvmd pid%d] %02d/%02d %02d:%02d:%02d %s", pvmmyupid,
  416.                     tmp->tm_mon + 1,
  417.                     tmp->tm_mday,
  418.                     tmp->tm_hour,
  419.                     tmp->tm_min,
  420.                     tmp->tm_sec,
  421.                     s);
  422.  
  423. #else    /*TIMESTAMPLOG*/
  424.         if (pvmmytid)
  425.             fprintf(stderr, "[t%x] %s", pvmmytid, s);
  426.         else
  427.             fprintf(stderr, "[pvmd pid%d] %s", pvmmyupid, s);
  428. #endif    /*TIMESTAMPLOG*/
  429.     }
  430.  
  431.     if (log_how & 2) {
  432.         if (log_alrdy < pvmdlogmax) {
  433. #ifdef    TIMESTAMPLOG
  434.             if (pvmmytid)
  435.                 sprintf(buf, "[t%x] %02d/%02d %02d:%02d:%02d ", pvmmytid,
  436.                         tmp->tm_mon + 1,
  437.                         tmp->tm_mday,
  438.                         tmp->tm_hour,
  439.                         tmp->tm_min,
  440.                         tmp->tm_sec);
  441.             else
  442.                 sprintf(buf, "[pvmd pid%d] %02d/%02d %02d:%02d:%02d ", pvmmyupid,
  443.                         tmp->tm_mon + 1,
  444.                         tmp->tm_mday,
  445.                         tmp->tm_hour,
  446.                         tmp->tm_min,
  447.                         tmp->tm_sec);
  448.  
  449. #else    /*TIMESTAMPLOG*/
  450.             if (pvmmytid)
  451.                 sprintf(buf, "[t%x] ", pvmmytid);
  452.             else
  453.                 sprintf(buf, "[pvmd pid%d] ", pvmmyupid);
  454. #endif    /*TIMESTAMPLOG*/
  455.  
  456.             l = strlen(buf);
  457.             (void)write(log_fd, buf, l);
  458.             (void)write(log_fd, s, len);
  459.             if ((log_alrdy += len + l) >= pvmdlogmax)
  460.                 (void)write(log_fd, toomuch, strlen(toomuch));
  461.         }
  462.     }
  463.  
  464. /*
  465.     if ((log_how & 4) && hosts->ht_local != hosts->ht_cons) {
  466. XXX send error-log message to console host
  467.     }
  468. */
  469. #endif
  470. }
  471.  
  472.  
  473.